package com.jeff.android.lab.android.lifecycle.lifecycle;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleRegistry;

import com.jeff.android.lab.BaseActivity;
import com.jeff.android.lab.util.RouterUtil;

public class LifeCycleActivity extends BaseActivity {

    private LifecycleRegistry mLifecycleRegistry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutView());

        mLifecycleRegistry = new LifecycleRegistry(this);
        mLifecycleRegistry.markState(Lifecycle.State.CREATED);

        getLifecycle().addObserver(new Java7LifecycleObserver());
        getLifecycle().addObserver(new Java8LifecycleObserver());
    }

    private View getLayoutView() {
        final LinearLayout root = new LinearLayout(this);
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        root.setLayoutParams(p);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setFitsSystemWindows(true);

        Button btn = new Button(this);
        btn.setText("自定义 LifeCycle");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterUtil.launchActivity(LifeCycleActivity.this, LifeCycleOldActivity.class);
            }
        });
        root.addView(btn);

        return root;
    }

}
